package com.peng.leetcode.unionFind;

import java.util.*;

/**
 * AccountsMerge
 * 721. 账户合并
 *
 * 给定一个列表 accounts，每个元素 accounts[i] 是一个字符串列表，其中第一个元素 accounts[i][0] 是 名称 (name)，其余元素是 emails 表示该账户的邮箱地址。
 *
 * 现在，我们想合并这些账户。如果两个账户都有一些共同的邮箱地址，则两个账户必定属于同一个人。请注意，即使两个账户具有相同的名称，它们也可能属于不同的人，因为人们可能具有相同的名称。一个人最初可以拥有任意数量的账户，但其所有账户都具有相同的名称。
 *
 * 合并账户后，按以下格式返回账户：每个账户的第一个元素是名称，其余元素是按顺序排列的邮箱地址。账户本身可以以任意顺序返回。
 *
 * 链接：https://leetcode-cn.com/problems/accounts-merge
 *
 * @author: lupeng6
 * @create: 2021/1/12 19:28
 */
public class AccountsMerge {

    public static void main(String[] args) {
        String[][] accounts = {{"John", "johnsmith@mail.com", "john00@mail.com"}, {"John", "johnnybravo@mail.com"}, {"John", "johnsmith@mail.com", "john_newyork@mail.com"}, {"Mary", "mary@mail.com"}};
        List<List<String>> list = new ArrayList<>();
        for (String[] account : accounts) {
            list.add(new ArrayList(Arrays.asList(account)));
        }
        List<List<String>> lists = new AccountsMerge().accountsMerge(list);

    }

    private Map<String, String> parent = new HashMap<>();

    public List<List<String>> accountsMerge(List<List<String>> accounts) {

        Map<String, String> mailMapName = new HashMap<>();
        for (List<String> account : accounts) {
            String name = account.get(0);
            for (int i = 1; i < account.size(); i++) {
                String email = account.get(i);
                mailMapName.put(email, name);
                parent.put(email, email);
            }
        }

        for (List<String> account : accounts) {
            if (account.size() > 2) {
                String mail = account.get(1);
                for (int i = 2; i < account.size(); i++) {
                    String nextMail = account.get(i);
                    union(mail, nextMail);
                }
            }
        }

        Map<String, Set<Integer>> mailMapIds = new HashMap<>();

        for (int i = 0; i < accounts.size(); i++) {
            List<String> account = accounts.get(i);

            for (int j = 1; j < account.size(); j++) {
                String mail = account.get(j);
                String mailRoot = find(mail);
                mailMapIds.putIfAbsent(mailRoot, new HashSet<>());
                mailMapIds.get(mailRoot).add(i);
            }
        }

        List<List<String>> lists = new ArrayList<>();

        for (Map.Entry<String, Set<Integer>> entry : mailMapIds.entrySet()) {
            Set<Integer> ids = entry.getValue();
            LinkedList<String> accountItem = new LinkedList<>();

            for (Integer id : ids) {
                List<String> account = accounts.get(id);
                for (int i = 1; i < account.size(); i++) {
                    String mail = account.get(i);
                    if (!accountItem.contains(mail)) {
                        accountItem.add(mail);
                    }
                }
            }
            Collections.sort(accountItem);

            for (Integer id : ids) {
                List<String> account = accounts.get(id);
                accountItem.push(account.get(0));
                break;
            }

            lists.add(accountItem);
        }

        Collections.sort(lists, new Comparator<List<String>>() {
            @Override
            public int compare(List<String> o1, List<String> o2) {
                return o1.get(0).compareTo(o2.get(0));
            }
        });

        return lists;

    }

    private void union(String m1, String m2) {
        String root1 = find(m1);
        String root2 = find(m2);
        if (!root1.equals(root2)) {
            parent.put(root1, root2);
        }
    }


    private String find(String mail) {
        while (!mail.equals(parent.get(mail))) {
            parent.put(mail, parent.get(mail));
            mail = parent.get(mail);
        }
        return mail;
    }
}
